
Ext.define('Tree',{
	extend:'Ext.data.Model',
	fields:[
		{name:'id', type:'int'},
		{name:'text', type:'string'},
		{name:'type', type:'int'}
	]	
});

function selectPanel(textName, _url){
	//var orgId = document.getElementById('orgId').value;

	var _store = Ext.create('Ext.data.TreeStore',{
		autoLoad: true,
		model:'Tree',
	    root: {
	        text: textName,
	        id:  opts.orgId,
	        expanded: true
	    },
	    proxy: {
	    	type: 'ajax',
	    	url: _url,
	    	extraParams :{
	    		orgId:''
	    	}
	    //,
	    //	root:'list'
	    },
	    listeners : {
	   		beforeexpand : function(node, eOpts){  
				
       			//点击父亲节点的菜单会将节点的id通过ajax请求，将到后台  
                this.proxy.extraParams.orgId = node.raw.id;  
            }
	    }
	});
	
	var _tree = Ext.create('Ext.tree.Panel',{
	    store: _store,
	    rootVisible: true,
	    autoScroll : true,
	    containerScroll: true,
	    height:324,
		listeners:{  
			itemdblclick:function(view, record, item, idx, event, eOpts) {
            	
				if(!_selStore.getNodeById(record.get('id'))){
					if(record.get('type')>0){
						var _wftype = Ext.get("wftype").dom.value;
						var _flowName = Ext.get("baseName").dom.value;
						var _respectOrgsId = record.get('id');
						$.ajax({  
				        	url:"/workflow/pdf.shtml?method=check", 
				        	type:'post',
				        	data:{
								'respectOrgsId':_respectOrgsId,
								'wftype':_wftype,
								'flowName':_flowName
							},	
				        	success:function(data){   
				            	if(data){ 
				            	Ext.MessageBox.confirm('提示：该组织已经存在相关联的流程定义','是否重新关联到此流程定义中',function(e){
				            			if(e=="yes"){
				            				_selTree.getRootNode().appendChild({
			            						id:record.get('id'),
			            						text:record.get('text'),
			            						leaf:true
			            					});
				            				Ext.Msg.alert('提示', '重新关联成功!');
				            			}
				                    });
		                        }else{
		                        	_selTree.getRootNode().appendChild({
								    	id:record.get('id'),
								    	text:record.get('text'),
								    	leaf:true
								    });
		                        }     
		                   }  
		                });
						
					} else {
						Ext.Msg.alert('提示','行业类型不可选');
					}
					
				}
	    	}
	    	
	    }
	});
	
	//已经选中组织机构树
	var _selStore = Ext.create('Ext.data.TreeStore',{
		autoLoad: true,
	    root: {
	        text: textName,
	        id: '1',
	        expanded: true
	    }
	});
	
	var _selTree = Ext.create('Ext.tree.Panel',{
		rootVisible: true,
	    store: _selStore,
	    height:324,
	    border:true,
	    listeners:{
	    	itemdblclick:function(view, record, item, idx, event, eOpts) {//tree双击事件
	    		record.remove();
	    	}
	    	
	    }
	});
	
	var _panel = Ext.create('Ext.Panel', {
		layout: 'border',
		width: 690,
	    height: 325,
		border:false,
		defaults: {
	        split: false,                 //是否有分割线
	        collapsible: false           //是否可以折叠
	    
	    },
	    items: [{
	    	border:false,
	    	bodyStyle: 'background:red;',
	    	region: 'west',
	        width: 300,
	        items:_tree
	    }, {
	    	border:false,
	    	bodyStyle: 'background:red;',
	    	region: 'center',
	    	xtype: "panel",
	    	bodyStyle:'padding:10 10 10 10',
	    	items:[{
	    		xtype:'panel',
	    		border:false,
	    		height:115
	    	},{
	   
	    		xtype:'button',
	    		minWidth:70,
	    		text:'全删',
	    		listeners:{
	    			click:function(){
	    				_selStore.getRootNode().removeAll();
	    			}
	    		}   		
	    	},{
	    		xtype:'panel',
	    		border:false,
	    		height:10
	    	},{
	    		xtype:'button',
	    		minWidth:70,
	    		text:'移除',
	    		listeners:{
	    			click:function(){
	    				_selStore.getNodeById(_selTree.getSelectionModel().getSelection()[0].raw.id).remove();
	    			}
	    		}
	    	}]
	    }, {
	    	border:false,
	    	xtype: "panel",
	    	region: 'east',
	        width: 300,
	        items:_selTree
	    }]
	}); 

	return {
		panel:_panel,
		store:_store,
		tree:_tree,
		selStore:_selStore,
		selTree:_selTree
		
	};
}

//任务委派
var respectOrgPanel  = selectPanel('组织机构', 'platform/orgTree'); 

var selectNextOrg = Ext.create('Ext.Window', {
	id:"selectNextOrg",
	title:'关联所属机构',
    height: 400,
    width: 700,
    layout: 'fit',
    collapsed :false,
    closeAction:'hide',
    //maximized :true,
    modal:true,
    items:[respectOrgPanel.panel],
    listeners:{
		'close':function(){
			Ext.get(Ext.DomQuery.select(".key_btn_down")).removeCls("key_btn_down"); 
		}
	},
    buttons: [{
		text: '确定',
		handler: function(){
			saveNextOrg();
			selectNextOrg.close();
		}
	},{
    	text: '关闭',
    	handler: function(){
    		selectNextOrg.close();
    	}
	}]
});

//保存组织

function saveNextOrg(){
	var names = '';
	var respectOrgsId = '';
	respectOrgPanel.selStore.getRootNode().eachChild(function(child){
		respectOrgsId += child.get('id')+ '-'+child.get('text')+',';
//		names += child.get('text')+',';
	});

	$('#respectOrgsId').val(respectOrgsId.substring(0, respectOrgsId.length-1));
//	$('#respectOrgName').val(names.substring(0, names.length-1));
}
/**
function selectOrgUser(){
	selectNextOrg.show();
}
 */

function setRespectOrgs(){
	
	var respectOrgsId = $('#respectOrgsId').val();
	var _obj = null;
	var _store = respectOrgPanel.selStore;
	respectOrgPanel.selStore.getRootNode().removeAll();
	selectNextOrg.show();
	if(respectOrgsId != ""){
		var rids = 	respectOrgsId.split(",");
			for(var i = 0;i < rids.length; i++ ){
				if(rids[i]!=""){
					_obj = rids[i].split("-");
					if(!_store.getNodeById(_obj[0])){
						_store.getRootNode().appendChild({
					    	id:_obj[0],
					    	text:_obj[1],
					    	leaf:true
					    });
					}
				}
			}
	}
	
	
}